package cn.tedu.egmvc5.controller;

import cn.tedu.egmvc5.mapper.AddressMapper;
import cn.tedu.egmvc5.pojo.dto.AddressAddParam;
import cn.tedu.egmvc5.pojo.entity.Address;
import cn.tedu.egmvc5.pojo.vo.AddressInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/v1/address/")
public class AddressController {
    @Autowired
    AddressMapper addressMapper;
    @PostMapping("add")
    public String add(AddressAddParam addressAddParam){
        System.out.println("addressAddParam="+addressAddParam);
        Address adderss = new Address();
        BeanUtils.copyProperties(addressAddParam,adderss);
        int num = addressMapper.addAddress(adderss);
        System.out.println(num>0?"添加地址成功":"添加地址失败");
        return "添加地址成功";
    }
    @GetMapping("list")
    public List<AddressInfoVO> addressList(){
         List<AddressInfoVO> addressInfoVOs = addressMapper.addressList();
         return addressInfoVOs;
    }
    @PostMapping("del")
    public String del(Integer id){
        int num = addressMapper.delAddress(id);
        System.out.println(num>0?"删除地址成功":"删除地址失败");
        return "删除地址成功";
    }
}
